//
//  XMGNewFeatureViewController.m
//  彩票
//
//  Created by apple on 20/3/28.
//
//
//1.UICollectionView创建时必须得要指定布局方式
//2.UICollectionViewCell必须得要通过注册的方式进行创建
//3.UICollectionViewCell内部默认是没有任何子控件,如果想要添加内容,必须得要自定义cell
#import "XMGNewFeatureViewController.h"

#import "XMGNewFeatureCell.h"

#import "UIView+GRJFrame.h"
#define XMGItemCount 4
@interface XMGNewFeatureViewController ()


//上一个offsetX
@property (nonatomic, assign)CGFloat preOffsetX;

/** 注释*/
@property (nonatomic ,weak) UIImageView *guide;


@end

@implementation XMGNewFeatureViewController


- (instancetype)init {
    
    
    //[[NSUserDefaults standardUserDefaults] setObject:curVersion forKey:XMGVersion];
    
    //1.UICollectionView创建时必须得要指定布局方式
     UICollectionViewFlowLayout *flowL = [[UICollectionViewFlowLayout alloc] init];
    //设置每一个格子的大小
    flowL.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    //设置最小的行间距
    flowL.minimumLineSpacing = 0;
    //设置每个格子之间的距离
    flowL.minimumInteritemSpacing = 0;
    //设置滚动的方向
    flowL.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    return [super initWithCollectionViewLayout:flowL];
}





static NSString * const reuseIdentifier = @"Cell";
- (void)viewDidLoad {
    [super viewDidLoad];
    

    //2.UICollectionViewCell必须得要通过注册的方式进行创建
    [self.collectionView registerClass:[XMGNewFeatureCell class] forCellWithReuseIdentifier:reuseIdentifier];

    
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    
    //添加子控件
    [self setUp];
    
  
    


}

//添加子控件
- (void)setUp {
    
    //guideLine
    UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLine"]];
    imageV.grj_left -= 150;
    [self.collectionView addSubview:imageV];
    
    //guide1
    UIImageView *guide = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guide1"]];
    guide.grj_left += 50;
    [self.collectionView addSubview:guide];
    self.guide = guide;
    //guideSmallText1
    UIImageView *guideSmallText = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideSmallText1"]];
    
    guideSmallText.center = CGPointMake(self.collectionView.grj_width * 0.5, self.collectionView.grj_height * 0.7);
    [self.collectionView addSubview:guideSmallText];
    
    //guideLargeText
    UIImageView *guideLargeText = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLargeText1"]];
    
    guideLargeText.center = CGPointMake(self.collectionView.grj_width * 0.5, self.collectionView.grj_height * 0.8);
    [self.collectionView addSubview:guideLargeText];
    
}

//当scorllView减速完毕时调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    //NSLog(@"%f",scrollView.contentOffset.x);
    
    
    CGFloat offset = scrollView.contentOffset.x - self.preOffsetX;
     //NSLog(@"offset==%f",offset);
    self.guide.grj_left += 2 * offset;
    //移动guide
    [UIView animateWithDuration:0.5 animations:^{
        self.guide.grj_left -= offset;
    }];

    self.preOffsetX = scrollView.contentOffset.x;
    
    //求当前是第几页
    int page = scrollView.contentOffset.x / self.collectionView.grj_width;
    
    //NSLog(@"%d",page);
    
    //设置图片
    NSString *imageName = [NSString stringWithFormat:@"guide%d",page + 1];
    self.guide.image = [UIImage imageNamed:imageName];
    
}


#pragma mark <UICollectionViewDataSource>

//总共有多组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}


//每一组有多个个格子(item)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return XMGItemCount;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    //2.UICollectionViewCell必须得要通过注册的方式进行创建
    XMGNewFeatureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    NSString *imageName = [NSString stringWithFormat:@"guide%ldBackground568h",indexPath.item + 1];
    

    cell.image = [UIImage imageNamed:imageName];
    cell.backgroundColor = [UIColor redColor];
    
    //添加立即体验
    [cell setStartBtn:indexPath count:XMGItemCount];
    
    
    NSLog(@"-----%p",cell);
    
    
    return cell;
}


@end
